package dbu.dbase;

/**
 * dBASE Data Types.
 * Except for autoincrement fields, all types are initialized to binary zeroes. 
 * In addition, any fields which have been assigned a default property will contain the default value. 
 * @author otomov
 */
public enum DataTypes {

	/** No type - constraint */
	NoType(' ', 0),

	/** All OEM code page characters - padded with blanks to the width of the field. */
	Character('C', 1),

	/** Number stored as a string, right justified, and padded with blanks to the width of the field. */ 
	Numeric('N', 2),

	/** a string 	10 digits (bytes) representing a .DBT block number. The number is stored as a string, right justified and padded with blanks. */
	Memo('M', 3),

	/** 1 byte - initialized to 0x20 (space) otherwise T or F. */
	Logical('L', 4),

	/** 8 bytes - date stored as a string in the format YYYYMMDD. */
	Date('D', 5),

	/** Number stored as a string, right justified, and padded with blanks to the width of the field. */ 
	Float('F', 6),

	/** 10 digits (bytes) representing a .DBT block number. The number is stored as a string, right justified and padded with blanks. */
	OLE('G', 8),

	/** Binary, a string 	10 digits representing a .DBT block number. The number is stored as a string, right justified and padded with blanks. */
	Binary('B', 9),

	/** 4 bytes. Leftmost bit used to indicate sign, 0 negative. */
	Long('I', 11),

	/** 8 bytes - two longs, first for date, second for time.  The date is the number of days since  01/01/4713 BC. Time is hours * 3600000L + minutes * 60000L + Seconds * 1000L */
	Timestamp('@', 12),

	/** 8 bytes - no conversions, stored as a double. */
	Double('O', 13),

	/** Same as a Long */
	Autoincrement('+', 14);

	private char symbol;
	private byte index;

	private DataTypes(char symbol, int index) {
		this.symbol = symbol;
		this.index = (byte) index;
	}

	public char getSymbol() {
		return symbol;
	}

	public byte getIndex() {
		return index;
	}

	public static DataTypes getBySymbol(char symbol) {
		for (DataTypes dataType: values()) {
			if (dataType.getSymbol() == symbol) {
				return dataType;
			}
		}
		return null;
	}

	public static DataTypes getByIndex(byte index) {
		for (DataTypes dataType: values()) {
			if (dataType.getIndex() == index) {
				return dataType;
			}
		}
		return null;
	}
}
